package com.mjw.zstu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mjw.zstu.entity.*;
import com.mjw.zstu.mapper.*;
import com.mjw.zstu.resultDto.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.*;

@Service
public class EchartsService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    VisitorMapper visitorMapper;
    @Autowired
    ExhibitionCompanyMapper exhibitionCompanyMapper;
    @Autowired
    ExhibitonMapper exhibitonMapper;
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    CompanyMapper companyMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    public JsonResult exhibition_ten(){
        List<Map> l=visitorMapper.ten();
        for(Map i : l){
            int id=(int)i.get("exhibition_id");
           Map m= exhibitionCompanyMapper.getDetail(id);
           i.put("name",m.get("name"));
        }
        return new JsonResult(200,"获取成功",l);
    }
    public JsonResult product_ten(){
        List<Map> l=productMapper.ten();
        for (Map i : l){
            int id=(int)i.get("product_id");
           Map m= productMapper.detail(id);
           i.put("name",m.get("name")+"("+m.get("company_name")+")");
        }
        return new JsonResult(200,"获取成功",l);
    }
    public  JsonResult pieEx(){
        List<Map> l=exhibitonMapper.pieEx();
        int sum=0;
        for(Map i:l){
           int id=(int)i.get("type_id");
           Type t= typeMapper.selectById(id);
           i.put("name",t.getType_name());
           sum=sum+Long.valueOf(String.valueOf(i.get("value"))).intValue();
        }
        QueryWrapper<Exhibition> exhibitionQueryWrapper=new QueryWrapper<>();
        int total=exhibitonMapper.selectCount(exhibitionQueryWrapper);
        if(total>sum){
            Map m=new HashMap();
            m.put("value",total-sum);
            m.put("name","其他");
            l.add(m);
            return new JsonResult(200,"获取成功",l);
        }
        return new JsonResult(200,"获取成功",l);

    }
    public JsonResult Companycount(){
        QueryWrapper<Company> queryWrapper=new QueryWrapper<>();
        int sum=companyMapper.selectCount(queryWrapper);
        return new JsonResult(200,"获取成功",sum);
    }
    public JsonResult Usernum(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        int sum=userMapper.selectCount(queryWrapper);
        return new JsonResult(200,"获取成功",sum);
    }
    public JsonResult Exnum(){
        QueryWrapper<Exhibition> queryWrapper=new QueryWrapper<>();
        int sum=exhibitonMapper.selectCount(queryWrapper);
        return new JsonResult(200,"获取成功",sum);
    }
    public  JsonResult todayEx(){
        long t=System.currentTimeMillis();
        QueryWrapper<Exhibition> queryWrapper=new QueryWrapper<>();
        queryWrapper.ge("end_time",t);
        queryWrapper.lt("start_time",t);
        int sum=exhibitonMapper.selectCount(queryWrapper);
        return new JsonResult(200,"获取成功",sum);
    }
    public JsonResult registerUser(){
        Calendar calendar=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        long start=calendar.getTimeInMillis();
        long[] l=new long[7];
        l[0]=start;
        for(int i=1;i<7;i++){
            l[i]=l[i-1]-86400000;
        }
        int[] count=new int[7];
        for(int i=0;i<7;i++){
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.ge("create_time",l[i]);
            queryWrapper.le("create_time",l[i]+86400000);
            int c=userMapper.selectCount(queryWrapper);
            count[i]=c;
        }
        List<Map> re= new ArrayList<>();
        for(int i=6;i>=0;i--){
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(l[i]/1000, 0, ZoneOffset.ofHours(8));
           int month= localDateTime.getDayOfMonth();
            System.out.println(month);
            Map m=new HashMap();
            m.put("day",month);
            m.put("value",count[i]);
            re.add(m);

        }
//        l.add(start);
        return new JsonResult(200,"获取成功",re);
    }
    public JsonResult order(){
        Calendar calendar=Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        long start=calendar.getTimeInMillis();
        long[] l=new long[7];
        l[0]=start;
        for(int i=1;i<7;i++){
            l[i]=l[i-1]-86400000;
        }
        int[] count=new int[7];
        for(int i=0;i<7;i++){
            QueryWrapper<Orderlist> queryWrapper=new QueryWrapper<>();
            queryWrapper.ge("create_time",l[i]);
            queryWrapper.le("create_time",l[i]+86400000);
            int c=orderMapper.selectCount(queryWrapper);
            count[i]=c;
        }
        List<Map> re= new ArrayList<>();
        for(int i=6;i>=0;i--){
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(l[i]/1000, 0, ZoneOffset.ofHours(8));
            int month= localDateTime.getDayOfMonth();
            System.out.println(month);
            Map m=new HashMap();
            m.put("day",month);
            m.put("value",count[i]);
            re.add(m);

        }
//        l.add(start);
        return new JsonResult(200,"获取成功",re);
    }
}
